/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.contorller;

import com.apache.api.vo.ResultMsg;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * description:  资金小金库提交插件
 *
 * @author Hou Dayu 创建时间：2016-8-29
 */
@SuppressWarnings("serial")
public class FusServlet extends BaseHttpServlet {

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    protected void doInvoke(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String doCode = request.getParameter("doCode");

        String result = "";
        ResultMsg msg = null;
        Map<String, String> params = this.getParameterMap(request);
        String sysPass = ConfigUtil.getInstance().interfacePass();
        params.put("sysPass", sysPass);
        json2map1(params, request);//动态参数
        msg = (ResultMsg) PortalPubFactory.getInstance().getPlugin("fus")
                .doInvoke(request, response, params);
        if (null == msg) {
            msg = new ResultMsg("F", "通信请求失败");
        }
        result = JSONObject.fromObject(msg).toString();
        outputJson(result, response);
    }

    private String getQuery(Object obj) {
        if (null != obj) {
            return JSONObject.fromObject(obj).toString();
        } else {
            return "{}";
        }
    }

    /**
     * json转map
     */
    private void json2map1(Map<String, String> params, HttpServletRequest request) {
        LoginUser loginUser = this.getSessionUser(request);
        if (StrUtil.isNotNull(request.getParameter("sqlQuery"))) {
            JSONObject jsonObject = JSONObject.fromObject(request.getParameter("sqlQuery"));
            Map<String, String> map1 = JSONObject.fromObject(jsonObject);
            String selectUser = String.valueOf(map1.get("selectUser"));
            if ("true".equals(selectUser) && null != loginUser) {
                params.put("createUser", loginUser.getUserEname());
            }
            map1.remove("selectUser");
            params.putAll(map1);
        } else {
            if (null != loginUser) {
                params.put("createUser", loginUser.getUserEname());
            }
        }
    }
}
